package com.yycx.common.utils.bean;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.NameCode;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.JsonUtils;
import com.yycx.common.utils.RedisUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 校验工具类
 *
 * @author zyf
 */
@Component
@ConditionalOnBean(RedisUtils.class)
public class CommonBeanUtils {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private HttpServletRequest request;


    /**
     * 获取邮箱验证码
     *
     * @param email
     * @return
     */
    public String getEmailCode(String email) {
        return redisUtils.getString(CommonConstants.PRE_EMAIL + email);
    }


    /**
     * 获取短信验证码
     *
     * @param mobile
     * @return
     */
    public String getSmsCode(String mobile) {
        String code = redisUtils.getString(CommonConstants.PRE_SMS + mobile);
        return Optional.ofNullable(code).orElse(getEmailCode(mobile));
    }


    /**
     * 校验短信验证码
     *
     * @param mobile
     * @param smsCode
     * @return
     */
    public Boolean validSmsCode(String mobile, String smsCode) {
        if (FlymeUtils.isNotEmpty(smsCode) && smsCode.equals("-1")) {
            return true;
        }
        ApiAssert.isNotEmpty("验证码不能为空", smsCode);
        String v = getSmsCode(mobile);
        ApiAssert.isNotEmpty("验证码无效", v);
        ApiAssert.eq("验证码不正确", smsCode, v);
        return true;
    }

    /**
     * 校验邮箱验证码
     *
     * @param email
     * @param emailCode
     * @return
     */
    public Boolean validEmailCode(String email, String emailCode) {
        if (FlymeUtils.isNotEmpty(email) && emailCode.equals("-1")) {
            return true;
        }
        ApiAssert.isNotEmpty("验证码不能为空", emailCode);
        String v = getEmailCode(email);
        ApiAssert.isNotEmpty("验证码无效", v);
        ApiAssert.eq("验证码不正确", emailCode, v);
        return true;
    }

    /**
     * 获取状态字段描述
     *
     * @param stateField 键
     * @param value      字段
     * @return 值
     */
    public String getStateFieldLabel(String stateField, String value) {
        Map<Object, Object> dataMaps = redisUtils.getMapByProject("DICT_DATA");
        List<EntityMap> entityMap = (List<EntityMap>) dataMaps.get("stateField");
        if (FlymeUtils.isNotEmpty(entityMap)) {
            List<EntityMap> list = entityMap.stream().filter(s -> s.get("name").equals(stateField)).collect(Collectors.toList());
            if (FlymeUtils.isNotEmpty(list)) {
                EntityMap map = list.get(0);
                String json = map.get("code");
                List<NameCode> nameCodes = JsonUtils.json2list(json, NameCode.class);
                if (FlymeUtils.isNotEmpty(nameCodes)) {
                    return nameCodes.stream().filter(s -> s.getValue().equals(value)).findFirst().orElse(new NameCode()).getLabel();
                }
            }
        }
        return "操作";
    }

    /**
     * 获取缓存订单
     *
     * @param outTradeNo 键
     * @return 值
     */
    public ResultBody getOrder(String outTradeNo) {
        return outTradeNo == null ? null : (ResultBody) redisUtils.get("ORDER_" + outTradeNo);
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public Long getUserId() {
        String userId = request.getParameter("userId");
        if (FlymeUtils.isNotEmpty(userId)) {
            return Long.parseLong(userId);
        }
        return OpenHelper.getUserId();
    }

    /**
     * 获取企业ID
     *
     * @return
     */
    public Long getCompanyId() {
        String companyId = request.getParameter("companyId");
        if (FlymeUtils.isNotEmpty(companyId)) {
            return Long.parseLong(companyId);
        }
        return OpenHelper.getCompanyId();
    }
}
